import api from "@/api"
export default {
    state: {
        activityInfo: uni.getStorageSync("activityInfo") || {},
        wss: uni.getStorageSync("wss") || "",
        token: uni.getStorageSync("token") || "",
        userInfo: uni.getStorageSync("userInfo") || ""
    },
    mutations: {
        setActivityInfo(state, info) {
            uni.setStorageSync("activityInfo", Object.assign({}, state.activityInfo || {}, info))
            state.activityInfo = Object.assign({}, state.activityInfo || {}, info)
        },
        setToken(state, token) {
            state.token = token
            uni.setStorageSync("token", token)
        },
        setWss(state, wss) {
            state.wss = wss
            uni.setStorageSync("wss", wss)
        },
        setUserInfo(state, user) {
            uni.setStorageSync("userInfo", user)
            state.userInfo = user
        },
        resetLogin(state) {
            uni.removeStorageSync("token")
            state.token = ""
        }
    },
    actions: {
        async userLogin({ commit, dispatch }, { method, ...payload }) {
            return new Promise((reslove, reject) => {
                api[method](payload)
                    .then(({ token, wss }) => {
                        commit("setToken", token)
                        commit("setWss", wss)
                        dispatch("getUserInfo")
                        reslove()
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        },
        getUserInfo({ commit }) {
            return new Promise(reslove => {
                api.getUserInfo().then(res => {
                    commit("setUserInfo", res)
                    reslove(res)
                })
            })
        }
    },
    getters: {}
}
